package entities

import (
	"bytes"
	"gitee.com/igolang/pkg/xhttp"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"net/http"
)

type Export struct {
	BlobType string
	Charset  string
	Buffer   *bytes.Buffer
}

func (e *Export) Stream(ctx *gin.Context) {
	contentType := xhttp.BlobType[e.BlobType]
	if contentType == "" {
		contentType = xhttp.MIMEOctetStream
	}
	if e.Charset != "" {
		contentType = contentType + ";charset=" + e.Charset
	}
	ctx.Header(xhttp.HeaderContentType, contentType)
	ctx.Header(xhttp.HeaderContentLength, cast.ToString(e.Buffer.Len()))
	ctx.Status(http.StatusOK)
	ctx.Writer.WriteHeaderNow()
	ctx.Writer.Write(e.Buffer.Bytes())
}
